import { Component, OnInit } from '@angular/core';
import { PageEntity } from '../../../core/domain/page.entity';
import { StatusEnum } from '../../../core/enum/status.enum';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {FilterEnum, FilterTypeEnum} from '../../../core/enum/filter.enum';
import {SystemReceiptService} from './system-receipt.service';
import {StorageUtil} from '../../../utils/storage.util';
import {PostDown} from '../../../utils/postdown';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-system-receipt',
  templateUrl: './system-receipt.component.html',
  styleUrls: ['./system-receipt.component.scss'],
  providers: [SystemReceiptService, PostDown]
})
export class SystemReceiptComponent implements OnInit {
  widthConfigs = [
    '60px', '180px', '200px', '150px', '150px',
    '150px', '150px', '150px', '150px', '180px', '150px', '150px', '180px'
  ];
  pageInfo: PageEntity = new PageEntity();
  allList = [];
  zydxList = [
    {
      text: '中干'
    },
    {
      text: '老师'
    },
    {
      text: '学生'
    }
  ];
  pageIndex = 1;
  currObj = null;
  searchObj: SearObj = new SearObj();
  stateObj = {
    0: '新增',
    5: '修改',
    10: '停用'
  };

  isVisible = false;
  teacherList = [];
  selTeacher = [];
  currId = null;

  constructor(
    private service: SystemReceiptService,
    private postDown: PostDown,
    private message: NzMessageService,
  ) { }

  ngOnInit() {
    this.resetSearch();
  }

  // 分页查询
  getPage() {
    this.pageInfo.filters.publishState = PaginationUtils.filters(10, FilterEnum.NOTEQ);
    this.pageInfo.filters.isExecute = PaginationUtils.filters(1, FilterEnum.EQUALS);
    this.pageInfo.multiSortMeta = [
      {
        field: 'gmtCreate',
        order: -1
      }
    ];
    this.service.page(this.pageInfo).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.allList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  // 第几页
  searchPageIndex(event) {
    this.pageInfo.first = event;
    this.getPage();
  }

  // 每页几条
  searchPageSize(event) {
    this.pageInfo.rows = event;
    this.pageInfo.first = 1;
    this.getPage();
  }

  backthis(e) {
    if (e) {
      this.getPage();
    }
    this.pageIndex = 1;
  }

  clickSearch() {
    for (const o in this.searchObj) {
      if (this.searchObj[o]) {
        if (o == 'publishDeptId') {
          this.pageInfo.filters[o] = PaginationUtils.filters(this.searchObj[o], FilterEnum.EQUALS);
        } else {
          this.pageInfo.filters[o] = PaginationUtils.filters(this.searchObj[o], FilterEnum.CONTAINS);
        }
      } else {
        delete this.pageInfo.filters[o];
      }
    }
    this.pageInfo.first = 1;
    this.getPage();
  }

  resetSearch() {
    this.searchObj = new SearObj();
    this.pageInfo = new PageEntity();
    this.getPage();
  }

  // 查看
  look(data) {
    this.currObj = data;
    this.pageIndex = 2;
  }

  // 执行分配
  zxfp(data) {
    this.currId = data.id;
    this.selTeacher = [];
    this.getTeachList();
  }

  // 教师数据查询
  getTeachList() {
    const urser = StorageUtil.getLocalStorage('user');
    const ft = {
      orgId: {
        matchMode: FilterEnum.EQUALS,
        value: urser.teacher.orgId,
        whereType: FilterTypeEnum.AND
      }
    };
    this.service.getTeachList(ft).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.teacherList = res.datas;
        this.service.getExecute({
          id: this.currId
        }).subscribe(res1 => {
          if (res1.status === StatusEnum.SUCCESS) {
            if (res1.datas && res1.datas.length > 0) {
              res1.datas.forEach(r1 => {
                this.selTeacher.push(r1.userId);
              });
            }
            this.isVisible = true;
          }
        });
      }
    });
  }

  submitFp() {
    if (this.selTeacher.length <= 0) {
      this.message.warning('执行分配用户不能为空!');
      return;
    }
    const tmparry = this.selTeacher.map(item => this.teacherList.filter(
      i => i.id == item)[0]);

    const tmparry1 = [];
    tmparry.forEach(item1 => {
      const obj = {
        userId: item1.id,
        userName: item1.name
      };
      tmparry1.push(obj);
    });
    this.service.executes({
      insId: this.currId,
      executeList: tmparry1
    }).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.isVisible = false;
        this.getPage();
      }
    });
  }

  /*附件下载*/
  downFj(data) {
    if (data.sourceId) {
      this.postDown.postDownload('security-server/dietc/sys/source/download', {id: data.sourceId});
    } else {
      this.message.warning('无附件！');
    }
  }

}

class SearObj {
  number?: string;
  name?: string;
  actionTarget?: string;
  publisherName?: string;
  deptName?: string;
}

